﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Nate_sRoguelikeGame
{
    static class Game
    {
        public static Player Player;
        public static List<Level> Levels = new List<Level>();
        public static Level CurrentLevel;
        public static Random Random = new Random();
        public static int CurrentTurn { get; private set;}
        private static QueuedInput queuedInput = null;
        static bool _quitGame = false;
        public static Point DrawingOffset = new Point(0, 2);
        /// <summary>
        /// Returns true to quit the game
        /// </summary>
        /// <returns></returns>
        public static void HandleInput(ConsoleKeyInfo key)
        {
            switch (key.Key)
            {
                case ConsoleKey.Spacebar:
                    CurrentLevel = Level.GenerateLevel("Level1", new Rectangle(0, 0, 80, 22), GameArchitecture.StoneWall, GameArchitecture.Stone, GameArchitecture.StoneWall,
                                                       false, true,
                                                       null);
                    break;
                case ConsoleKey.Q:
                    Stop();
                    return;
                case ConsoleKey.NumPad4:
                case ConsoleKey.NumPad7:
                case ConsoleKey.NumPad8:
                case ConsoleKey.NumPad9:
                case ConsoleKey.NumPad6:
                case ConsoleKey.NumPad3:
                case ConsoleKey.NumPad2:
                case ConsoleKey.NumPad1:
                case ConsoleKey.NumPad5:
                    Player.Move(GameDirection.GetDirection(key.Key));
                    break;
                case ConsoleKey.W:
                    if (key.Modifiers == ConsoleModifiers.Shift)
                    {
                        // Wear item
                    }
                    else
                    {
                        queuedInput = new QueuedInput(GameDirection.PromptForDirection(), -1);
                    }
                    break;
            }
        }
        // Resets the cursor to the player's location
        public static void ResetCursor()
        {
            Console.SetCursorPosition(Player.MapBlock.Location.X + DrawingOffset.X, Player.MapBlock.Location.Y + DrawingOffset.Y);
        }
        public static int Roll(int dice, int sides, bool isZeroBased = false)
        {
            return Roll(dice, sides, 0, isZeroBased);
        }

        public static int Roll(int dice, int sides, int minRoll, bool isZeroBased = false)
        {
            int result = 0;
            for (int i = 0; i < dice; i++)
                result += Random.Next(sides + (isZeroBased ? 0 : 1));
            return Math.Max(minRoll, result);
        }

        public static void Run()
        {
            Console.CursorSize = 5;
            
            while (!_quitGame)
            {
                CurrentLevel.Draw();
                for (int i = 0; i < CurrentLevel.RoomCountsBySize.Length; i++)
                {
                    Console.SetCursorPosition(0, i);
                    Console.Write("{0} Rooms: {1}", (RoomSize)i, CurrentLevel.RoomCountsBySize[i]);
                }
                Console.SetCursorPosition(0, 24);
                Console.Write("Turn: {0}", CurrentTurn);
                ResetCursor();
                
                // Use queued input first
                
                ConsoleKeyInfo key;
                if (Player.IsDisturbed)
                    queuedInput = null;
                if (queuedInput != null)
                {
                    key = queuedInput.Key; 
                    if (queuedInput.HasExpired)
                        queuedInput = null;
                }
                else key = Console.ReadKey(true);
                Player.ScanArea();
                HandleInput(key);
                CurrentTurn++;
                
            }
        }

        public static void Stop()
        {
            _quitGame = true;
        }
    }
}
